#include "../include/Hud.hpp"
#include "../include/SpritesManager.hpp"
#include "../include/Hero.hpp"
#include "stdio.h"
#include <algorithm>

Hud::Hud(SpritesManager* _spritesManager, std::vector<int> forbiddenTransfos){
	spritesManager = _spritesManager;
	lastHealthValue = 0;
	lastManaValue = 0;
	lastTransformId = 0;
	initialize(forbiddenTransfos);
}

Hud::~Hud(void){

	for(unsigned int i =0; i< progressBar.size(); i++){

		delete progressBar.at(i);
	}
	for(unsigned int i =0; i< healthBar.size(); i++){

		delete healthBar.at(i);
	}
	for(unsigned int i =0; i< manaBar.size(); i++){

		delete manaBar.at(i);
	}
	for(unsigned int i =0; i< shapeShiftIcons.size(); i++){

		delete shapeShiftIcons.at(i);
	}
}

void Hud::update(int scrollValue, int pursuerScrollValue, int levelSize, std::vector<int> forbiddenTransfos){

	//iprintf("position marqueur %d\n", spritesManager->oamSub->oamBuffer[rithMarkerOAM_ID].x);
	levelSize += 50;
	scrollValue += 50;
	pursuerScrollValue +=50;

	/* Update the position of the markers on the progress bar */
	spritesManager->oamSub->oamBuffer[enemyMarkerOAM_ID].x = ((float)pursuerScrollValue/(float)levelSize)*160.0+32.0;
	spritesManager->oamSub->oamBuffer[rithMarkerOAM_ID].x = ((float)scrollValue/(float)levelSize)*160.0+32.0;

	/* Update health */
	if(lastHealthValue!=(spritesManager->hero->getHealth())){
		lastHealthValue = spritesManager->hero->getHealth();
		for(unsigned int i=1; i<lastHealthValue+1; i++){
			spritesManager->oamSub->oamBuffer[healthBar.at((i*2)-1)->OAM_ID].isHidden = false;
			spritesManager->oamSub->oamBuffer[healthBar.at(i*2)->OAM_ID].isHidden = true;
		}
		for(unsigned int j=lastHealthValue+1; j<spritesManager->hero->getHealthMax()+1; j++){
			spritesManager->oamSub->oamBuffer[healthBar.at((j*2)-1)->OAM_ID].isHidden = true;
			spritesManager->oamSub->oamBuffer[healthBar.at(j*2)->OAM_ID].isHidden = false;
		}
	}

	/* Update Mana */
	if(lastManaValue!=(spritesManager->hero->getMana())){
		lastManaValue = spritesManager->hero->getMana();
		for(unsigned int i=1; i<lastManaValue+1; i++){
			spritesManager->oamSub->oamBuffer[manaBar.at((i*2)-1)->OAM_ID].isHidden = true;
			spritesManager->oamSub->oamBuffer[manaBar.at(i*2)->OAM_ID].isHidden = false;
		}
		for(unsigned int j=lastManaValue+1; j<spritesManager->hero->getManaMax()+1; j++){
			spritesManager->oamSub->oamBuffer[manaBar.at((j*2)-1)->OAM_ID].isHidden = false;
			spritesManager->oamSub->oamBuffer[manaBar.at(j*2)->OAM_ID].isHidden = true;
		}
	}
	
	/* Update Shape Shift icons*/
	if(lastTransformId!=(spritesManager->hero->getTransformID())){
		lastTransformId = spritesManager->hero->getTransformID();
		for(unsigned int i=0; i<shapeShiftIcons.size();i++)spritesManager->oam->oamBuffer[shapeShiftIcons.at(i)->OAM_ID].isHidden = true;
		std::vector<int>::iterator it;
		switch (lastTransformId){
			case 0:
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 1);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(0)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(2)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(8)->OAM_ID].isHidden = false;
				}
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 2);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(1)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(3)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(9)->OAM_ID].isHidden = false;
				}
				break;
			case 1:
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 3);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(1)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(4)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(9)->OAM_ID].isHidden = false;
				}
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 5);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(5)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(8)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(0)->OAM_ID].isHidden = false;
				}
				break;
			case 2:
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 6);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(0)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(6)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(8)->OAM_ID].isHidden = false;
				}
				it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 4);
				if (it == forbiddenTransfos.end()){
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(1)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(7)->OAM_ID].isHidden = false;
					spritesManager->oam->oamBuffer[shapeShiftIcons.at(9)->OAM_ID].isHidden = false;
				}
				break;
			default:
				break;
		}
	}


}

void Hud::initialize(std::vector<int> forbiddenTransfos){
	

	/* Display the markers on the progress bar */
//	Sprite* rithMarker = new Sprite();
	Sprite rithMarker;
	rithMarker.display(spritesManager, TOP, 100, 144, OBJPRIORITY_0, spritesManager->PROGRESS_MARKER_RITH, spritesManager->PROGRESS_MARKER_RITH_PAL, OTHER);
	rithMarkerOAM_ID = rithMarker.OAM_ID;

//	Sprite* enemyMarker = new Sprite();
	Sprite enemyMarker;
	enemyMarker.display(spritesManager, TOP, 50, 144, OBJPRIORITY_0, spritesManager->PROGRESS_MARKER_ENEMY, spritesManager->PROGRESS_MARKER_ENEMY_PAL, OTHER);
	enemyMarkerOAM_ID = enemyMarker.OAM_ID;

	/* Display the progress bar */
	for(unsigned int i=0; i<5; i++){
		progressBar.push_back(new Sprite());
		progressBar.back()->display(spritesManager, TOP, 32 + i*32, 160, OBJPRIORITY_0, spritesManager->PROGRESS_BAR_BLOC, spritesManager->PROGRESS_BAR_BLOC_PAL, OTHER);
	}
	progressBar.push_back(new Sprite());
	progressBar.back()->display(spritesManager, TOP, 192, 160, OBJPRIORITY_0, spritesManager->PROGRESS_BAR_END, spritesManager->PROGRESS_BAR_END_PAL, OTHER);
	
	/* Display health */
	healthBar.push_back(new Sprite());
	healthBar.back()->display(spritesManager, TOP, 0, 0, OBJPRIORITY_0, spritesManager->HEALTH_BAR_BEG, spritesManager->HEALTH_BAR_PAL, OTHER);
	for(unsigned int i=0; i<3; i++){
		healthBar.push_back(new Sprite());
		healthBar.back()->display(spritesManager, TOP, 32 + i*32, 0, OBJPRIORITY_0, spritesManager->HEALTH_BAR_BLOC, spritesManager->HEALTH_BAR_PAL, OTHER);
		spritesManager->oamSub->oamBuffer[healthBar.back()->OAM_ID].isHidden = true;
		healthBar.push_back(new Sprite());
		healthBar.back()->display(spritesManager, TOP, 32 + i*32, 0, OBJPRIORITY_0, spritesManager->HEALTH_BAR_EMPTY, spritesManager->HEALTH_BAR_PAL, OTHER);
	}
	healthBar.push_back(new Sprite());
	healthBar.back()->display(spritesManager, TOP, 128, 0, OBJPRIORITY_0, spritesManager->HEALTH_BAR_END, spritesManager->HEALTH_BAR_PAL, OTHER);

	/* Display mana */
	manaBar.push_back(new Sprite());
	manaBar.back()->display(spritesManager, TOP, 0, 32, OBJPRIORITY_0, spritesManager->MANA_BAR_BEG, spritesManager->MANA_BAR_PAL, OTHER);
	for(unsigned int i=0; i<3; i++){
		manaBar.push_back(new Sprite());
		manaBar.back()->display(spritesManager, TOP, 32 + i*32, 32, OBJPRIORITY_0, spritesManager->MANA_BAR_EMPTY, spritesManager->MANA_BAR_PAL, OTHER);
		manaBar.push_back(new Sprite());
		manaBar.back()->display(spritesManager, TOP, 32 + i*32, 32, OBJPRIORITY_0, spritesManager->MANA_BAR_BLOC, spritesManager->MANA_BAR_PAL, OTHER);
		spritesManager->oamSub->oamBuffer[manaBar.back()->OAM_ID].isHidden = true;
	}
	manaBar.push_back(new Sprite());
	manaBar.back()->display(spritesManager, TOP, 128, 32, OBJPRIORITY_0, spritesManager->MANA_BAR_END, spritesManager->MANA_BAR_PAL, OTHER);

	/* Display shape shift icons */
	shapeShiftIcons.push_back(new Sprite());
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 186, 6, OBJPRIORITY_0, spritesManager->SHAPESHIFT_ICON_UP, spritesManager->SHAPESHIFT_ICON_PAL, OTHER);
	shapeShiftIcons.push_back(new Sprite());
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 186, 39, OBJPRIORITY_0, spritesManager->SHAPESHIFT_ICON_DOWN, spritesManager->SHAPESHIFT_ICON_PAL, OTHER);

	std::vector<int>::iterator it;
	// Vampire vector index : 2
	shapeShiftIcons.push_back(new Sprite());
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 6, OBJPRIORITY_0, spritesManager->VAMPIRE_1, spritesManager->VAMPIRE_PAL, OTHER);
	
	// Zombie vector index : 3
	shapeShiftIcons.push_back(new Sprite());	
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 39, OBJPRIORITY_0, spritesManager->ZOMBIE_1, spritesManager->ZOMBIE_PAL, OTHER);
	// Bat vector index : 4
	shapeShiftIcons.push_back(new Sprite());	
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 39, OBJPRIORITY_0, spritesManager->BAT_1, spritesManager->BAT_PAL, OTHER);
	spritesManager->oam->oamBuffer[shapeShiftIcons.back()->OAM_ID].isHidden = true;
	// Werewolf vector index : 5
	shapeShiftIcons.push_back(new Sprite());	
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 6, OBJPRIORITY_0, spritesManager->WEREWOLF_1, spritesManager->WEREWOLF_PAL, OTHER);
	spritesManager->oam->oamBuffer[shapeShiftIcons.back()->OAM_ID].isHidden = true;
	// Hunter vector index : 6
	shapeShiftIcons.push_back(new Sprite());	
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 6, OBJPRIORITY_0, spritesManager->HUNTER_1, spritesManager->HUNTER_PAL, OTHER);
	spritesManager->oam->oamBuffer[shapeShiftIcons.back()->OAM_ID].isHidden = true;
	// Fat Zombie vector index : 7
	shapeShiftIcons.push_back(new Sprite());	
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 39, OBJPRIORITY_0, spritesManager->FATZOMBIE_1, spritesManager->FATZOMBIE_PAL, OTHER);
	spritesManager->oam->oamBuffer[shapeShiftIcons.back()->OAM_ID].isHidden = true;
	
	shapeShiftIcons.push_back(new Sprite());
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 6, OBJPRIORITY_0, spritesManager->SHAPESHIFT_ICON_BLOC, spritesManager->SHAPESHIFT_ICON_PAL, OTHER);
	
	shapeShiftIcons.push_back(new Sprite());
	shapeShiftIcons.back()->display(spritesManager, BOTTOM, 218, 39, OBJPRIORITY_0, spritesManager->SHAPESHIFT_ICON_BLOC, spritesManager->SHAPESHIFT_ICON_PAL, OTHER);

	it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 1);
	if (it != forbiddenTransfos.end()){
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(0)->OAM_ID].isHidden = true;
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(2)->OAM_ID].isHidden = true;
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(8)->OAM_ID].isHidden = true;
	}
	it = std::find(forbiddenTransfos.begin(), forbiddenTransfos.end(), 2);
	if (it != forbiddenTransfos.end()){
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(1)->OAM_ID].isHidden = true;
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(3)->OAM_ID].isHidden = true;
		spritesManager->oam->oamBuffer[shapeShiftIcons.at(9)->OAM_ID].isHidden = true;
	}
}